<?php  
class AccessHelper extends Helper{  
    var $helpers = array("Session");  
    var $Access;  
    var $Auth;  
    var $user;
    
    const PLANET_LV_POINT = 1000;
    const STAR_LV_POINT = 3000;
  
    function beforeRender(){  
        App::import('Component', 'Access');  
        $this->Access = new AccessComponent();
  
        App::import('Component', 'Auth');  
        $this->Auth = new AuthComponent();  
        $this->Auth->Session = $this->Session;   
  
        $this->user = $this->Auth->user();  
        
        // Get loged in user info
        if(!empty($this->user)){
        $this->user['User']['points'] = $this->get_points($this->user['User']['id']);
        $this->user['User']['planet_notif'] = '';
        $this->user['User']['star_notif'] = '';
        if($this->user['User']['points']<AccessHelper::PLANET_LV_POINT)
            $this->user['User']['planet_notif'] = '<br>'.(AccessHelper::PLANET_LV_POINT-$this->user['User']['points']).' points until free Planet level ';
        if($this->user['User']['points']<AccessHelper::STAR_LV_POINT)
            $this->user['User']['star_notif'] = '<br>'.(AccessHelper::STAR_LV_POINT-$this->user['User']['points']).' points until free Planet level ';
        $userNPRofile = $this->getUserNProfile();
        $this->user['User']['image'] = $userNPRofile['UserProfile']['image'];
        }                
    }   
  
    function check($aco, $action='*'){  
        if(empty($this->user)) return false;  
        return $this->Access->checkHelper('User::'.$this->user['User']['id'], $aco, $action);  
    }   
  
    function isLoggedin(){  
        return !empty($this->user);  
    }  
    function getmy($what){  
        return !empty($this->user) && isset($this->user['User'][$what]) ? $this->user['User'][$what] : false;  
    }
    function get_points($id){
        if($id){
            $data=mysql_query("SELECT SUM(credit) as points from `user_credits` where `user_id`='$id'");
            $points=mysql_fetch_array($data);
            if($data){
            $points=$points['points'];
            }else { $points=0;}
        }else{
            $points=0;
        }
        return $points ;
    }
    
   function getUserNProfile(){
        $this->User=& ClassRegistry::init('User');        
        $this->User->unbindModel(array('hasMany' => array('UserCredit', 'Subject', 'SubjectRate')));
        $data = $this->User->find('first',array('conditions'=>array('User.id' => $this->Auth->User('id'))));
        return $data;
    }
}  
?>